package com.student.liu.controller;

import com.student.liu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * 请假申请 Controller
 */
@Slf4j
@RestController
@Api(tags = "报销相关接口")
@RequestMapping("admin/reimbursement")
public class ReimbursementController extends BaseController {

    @Resource
    IReimbursementService reimbursementService;
    @Resource
    IReimbursementCategoryService leaveApplyTypeService;
    @Resource
    IReimbursementStateService leaveApplyStateService;

    @Data
    static class MyLeaveApplyParamBody{
        @NotNull(message = "请传递页码：pageIndex")
        private Integer pageIndex;
        @Max(value = 12, message = "页大小不能超过12")
        @NotNull(message = "请传递页大小：pageSize")
        private Integer pageSize;
    }


    @PostMapping
    @ApiOperation("查看自己的报销表")
    public Object index(@Valid @RequestBody MyLeaveApplyParamBody paramBody, BindingResult result) {
        return success(null, reimbursementService.pageMyReimbursementInfo(paramBody.getPageIndex(),
                paramBody.getPageSize()));
    }

    @Data
    static class LeaveApplyParamBody extends MyLeaveApplyParamBody{
        private String username;
    }

    @PostMapping("all")
    @ApiOperation("查看报销表")
    @PreAuthorize("hasAnyRole('admin','ROLE_boss')")
    public Object all(@Valid @RequestBody LeaveApplyParamBody paramBody, BindingResult result) {
        return success(null, reimbursementService.pageReimbursementInfo(paramBody.getPageIndex(),
                paramBody.getPageSize(), paramBody.getUsername()));
    }

    @GetMapping("types")
    @ApiOperation("查看category表")
    public Object types() {
        return success(null, leaveApplyTypeService.list());
    }

    @GetMapping("states")
    @ApiOperation("查看state表")
    public Object states() {
        return success(null, leaveApplyStateService.list());
    }

    @Data
    static class LeaveApplySubmitParam {
        @NotNull(message = "请传递报销名类型")
        private String name;
        @NotNull(message = "请传递报销类型")
        private Byte categoryId;
        @NotNull(message = "请传递报销时间")
        private LocalDateTime reimbursetime;
        @NotNull(message = "请传递报销金额")
        private Integer money;
        @NotNull(message = "请传递报销产生的时间")
        private LocalDateTime producetime;
        @NotBlank(message = "请传递报销的原因")
        private String reason;  //请假原因
    }

    /**
     * 提交申请
     * @param param
     * @param result
     * @return
     */
    @PostMapping("submit")
    @ApiOperation("提交申请")
    public Object submit(@Valid @RequestBody LeaveApplySubmitParam param, BindingResult result) {

        reimbursementService.submitApply(param.getCategoryId(),param.getName(),param.getMoney(),param.getReimbursetime(),param.getProducetime() , param.getReason());
        return success("提交成功", null);
    }

    /**
     * 撤回申请
     * @param id
     * @return
     */
    @PostMapping("callback")
    @ApiOperation("撤回申请")
    public Object callback(@RequestParam Integer id) {
        reimbursementService.callbackApply(id);
        return success("撤回成功", null);
    }

    /**
     * 审批
     * @param processInstanceId
     * @return
     */
    @PostMapping("approval")
    @ApiOperation("审批接口")
    public Object approval(@RequestParam String processInstanceId) {
        return success(null, reimbursementService.approval(processInstanceId));
    }

    @Data
    static class ApprovalDisposeParam{
        @NotNull(message = "请传递报销记录ID")
        private Integer leaveApplyId;
        private String disposeReason;   //处理意见
        private String taskID;  //任务ID
    }

    /**
     *  审批通过
     * @param approvalDisposeParam
     * @param result
     * @return
     */
    @PostMapping("passDispose")
    @ApiOperation("审批通过")
    public Object passDispose(@Valid @RequestBody ApprovalDisposeParam approvalDisposeParam, BindingResult result) {
        reimbursementService.passDispose(approvalDisposeParam.getLeaveApplyId(),
                approvalDisposeParam.getDisposeReason(), approvalDisposeParam.getTaskID());
        return success("处理成功", null);
    }

    /**
     * 审批驳回
     * @param approvalDisposeParam
     * @param result
     * @return
     */
    @PostMapping("rejectDispose")
    @ApiOperation("审批驳回")
    public Object rejectDispose(@Valid @RequestBody ApprovalDisposeParam approvalDisposeParam, BindingResult result) {
        reimbursementService.rejectDispose(approvalDisposeParam.getLeaveApplyId(),
                approvalDisposeParam.getDisposeReason(), approvalDisposeParam.getTaskID());
        return success("处理成功", null);
    }

}
